"""
Some unittest for the contest application.
"""

from django.test import TestCase
from cassebras.contest.models import Category, Slope, Competitor, \
                                     SuccessfullClimb

class TestCategory(TestCase):
    """
    Class used to test the Category class
    """
    
    def test_score(self):
        """
        Test score update process that is fire everytime a 
        SuccessfullClimb is saved into the DataBase
        """
        slope = Slope(name='slope1')
        slope.save()
        cat = Category(name='Elite')
        cat.save()
        for i in range(2):
            competitor = Competitor(name=str(i), category=cat, number=i)
            competitor.save()
            #c.climber_slopes.add(s)
            success = SuccessfullClimb(competitor=competitor, slope=slope)
            success.save()
        slope = Slope.objects.get(name='slope1')
        self.failUnlessEqual(slope.score, 50)
        competitor = Competitor.objects.get(name=str(0))
        slope2 = Slope(name='slope2')
        slope2.save()
        success = SuccessfullClimb(competitor=competitor, slope=slope2)
        success.save()
        competitor = Competitor.objects.get(name=str(0))
        self.failUnlessEqual(competitor.score, 150)
        ranking = cat.rank()
        self.failUnlessEqual(ranking[0].score, competitor.score)

class TestSlope(TestCase):
    """Class used to test the Slope class"""
    def test_score(self):
        slope = Slope(name='slope')
        slope.save()
        self.failUnlessEqual(slope.score, Slope.INITIAL_SCORE)
        cat = Category(name='Elite')
        cat.save()
        comp = Competitor(name='marvin', category=cat, number=1)
        comp.save()
        success = SuccessfullClimb(competitor=comp, slope=slope)
        success.save()
        self.failUnlessEqual(slope.score, Slope.INITIAL_SCORE)
        success = SuccessfullClimb(competitor=comp, slope=slope)
        success.save()
        self.failUnlessEqual(slope.score, Slope.INITIAL_SCORE)
        comp = Competitor(name='another marvin', category=cat, number=2)
        comp.save()
        success = SuccessfullClimb(competitor=comp, slope=slope)
        success.save()
        self.failUnlessEqual(slope.score, Slope.INITIAL_SCORE/2)

